//app.js
App({
  onLaunch: function() {    
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 云函数
    wx.cloud.init({
      env: "hjf-0ql1k",
      traceUser: true
    })
    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力')
    } else {
      wx.cloud.init({
        traceUser: true,
      })
    }
  }, 
  onLoad: function() {},
  globalData: {
    userInfo: null,
    location: '',
    userId: '',
    userType: '', // 0:用户 1:营地端
    appId: 'wxffce1497b3bd775c',
    openId: '',
    city: '',
    address: '',
    buycar_num: 0,
    totalMoney: 0,
    totalSecond: 899,
    ip: '',
    testApiUrl: 'https://www.camphobby.com',
    statusBarHeight: wx.getSystemInfoSync()['statusBarHeight']
  },
  errorDate: {
    route: '',
    params: null,
    interface: '',
    statusCode: 0,
    backData: null
  },
  setLogCloud: function (data) {
    wx.cloud.callFunction({
      name: 'setLog',
      data: {
        openId: this.globalData.userInfo == null ? '' : this.globalData.openId,
        userId: this.globalData.userInfo == null ? '' : this.globalData.userId,
        userType: this.globalData.userInfo == null ? '' : this.globalData.userType,
        userName: this.globalData.userInfo == null ? '' : this.globalData.userInfo.nickName,
        route: this.errorDate.route == '' ? '' : this.errorDate.route,
        interface: this.errorDate.interface == '' ? '' : this.errorDate.interface,
        statusCode: this.errorDate.statusCode == '' ? '' : this.errorDate.statusCode,
        params: this.errorDate.params ? this.errorDate.params : '',
        backData: this.errorDate.backData ? this.errorDate.backData : ''
      }
    })
  },
  // 随机32位字符串
  randomWord: function () {
    var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
    var nums = "";
    for (var i = 0; i < 32; i++) {
      var id = parseInt(Math.random() * 61);
        nums += chars[id];
    }
    return nums;
  }
})
